package cn.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.pojo.Detail;
import cn.service.DetailService;

@Controller
@RequestMapping("/st")
public class DetailController {
	private static String pattern = "yyyy-MM-dd";

	@Resource
	private DetailService detailService;

	@RequestMapping(value = "/getAll", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAll() {
		List<Detail> list = detailService.getAll();
		String json = JSON.toJSONStringWithDateFormat(list, pattern, SerializerFeature.WriteDateUseDateFormat);
		return json;
	}

	// 测试用户名是否可用: testAuthor
	@RequestMapping(value = "/testAuthor", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String testAuthor(Detail detail) {
		detail = detailService.testAuthor(detail);
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		if (detail != null) {
			map.put("result", false);
		} else {
			map.put("result", true);
		}
		return JSON.toJSONString(map);
	}

	// ajax验证标题:testTitle
	@RequestMapping(value = "/testTitle", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String testTitle(Detail detail) {
		detail = detailService.testTitle(detail);
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		if (detail != null) {
			map.put("result", false);
		} else {
			map.put("result", true);
		}
		return JSON.toJSONString(map);
	}

	// 提交表单:toUserAdd
	@RequestMapping(value = "/toUserAdd", produces = "application/json;charset=utf-8")
	public String toUserAdd(Detail detail) {
		int num = detailService.toUserAdd(detail);
		System.out.println(detail);
		return "redirect:/Detail.jsp";
	}

}
